package operations;

import bookList.Book;
import bookList.BookList;

import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

/**
 * The type Del operations.
 *
 * @author CY
 * @date 2022 /08/09 23:43
 */
public class DelOperations implements Operations {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的书名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        Book[] books = Arrays.copyOf(bookList.getBooks(), bookList.getUsedSize());
        for (int i = 0; i < bookList.getBooks().length; i++) {
            if (Objects.equals(name, bookList.getBooks()[i].getName())) {
                System.out.println("删除成功，欢迎下次使用！");
                for (int j = i; j <= bookList.getUsedSize() - 2; j++) {
                    books[j] = books[j + 1];
                }
                bookList.getBooks()[bookList.getUsedSize() - 1] = null;
                bookList.setBooks(books);
                bookList.setUsedSize(bookList.getUsedSize() - 1);
                System.out.println();
                return;
            }
        }
        System.out.println("对不起，没有找到这本书！");
        System.out.println();


    }
}
